package com.goudan.rabbitmq.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MsgData<T> implements Serializable {
    private static final long serialVersionUID = -4699723280599750033L;
    private String code;
    private T data;
    private String message;

    public MsgData(T data) {
        this.data = data;
    }

    public MsgData(byte[] bytes, Class<T> clazz) {
        MsgData<T> data = JSON.parseObject(new String(bytes), new TypeReference<MsgData<T>>(clazz) {});
        this.code = data.code;
        this.data = (T) data.data;
        this.message = data.message;
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }
}
